package com.pattern.Iterator;

/**
 *  具体的集合
 */
public class BookShelf implements Aggregate {

    private Book[] books;
    private Integer last; // 指向当前对象的后一个位置

    public BookShelf(int maxSize) {
        books = new Book[maxSize];
        last = 0;
    }

    // 获取当前对象
    public Book getAt(int index) {
        if (index < books.length) {
            return books[index];
        } else {
            return null;
        }
    }

    // 添加
    public void append(Book book) {
        books[last] = book;
        last++;
    }

    public int getLength() {
        return last;
    }

    public Iterator iterator() {
        return new BookShelfIterator(this);
    }
}
